package com.bmps.springcloud.insuredmicroservice.service.underwrite.strategy.traffic;

import com.bmps.springcloud.common.utils.DateUtil;
import com.bmps.springcloud.insuredmicroservice.config.accept.ProductConfig;
import com.bmps.springcloud.insuredmicroservice.constant.accept.AcceptConstant;
import com.bmps.springcloud.insuredmicroservice.constant.accept.UnderWriteConstant;
import com.bmps.springcloud.insuredmicroservice.model.accept.resquest.*;
import com.bmps.springcloud.insuredmicroservice.model.underwrite.InsuredModel;
import com.bmps.springcloud.insuredmicroservice.repository.underwrite.coresystem.InsuredCoreRepository;
import com.bmps.springcloud.insuredmicroservice.service.RedisService;
import com.bmps.springcloud.insuredmicroservice.util.accept.AcceptUtil;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * description:
 * 风险保额 险种AAT070
 *
 * @author jackdaw
 * @date 2019-10-29 10:47
 */
public class Aat070TrafficStrategy implements TrafficStrategy {

    @Override
    public InsuredModel checkTrafficRolesByRiskCode(AcceptRequestDataModel acceptRequestDataModel, InsuredCoreRepository insuredCoreRepository, ProductModel productModel, RedisService redisService, ProductConfig productConfig) {
        RequestModel requestModel = acceptRequestDataModel.getRequest();
        List<InsurantModel> insurantModelList = requestModel.getCustInfo().getInsurants().getInsurant();
        int prodInsSeq = productModel.getProdInsSeq();
        String prtNo = requestModel.getPolicyInfo().getPrtNo();

        for (InsurantModel insurantModel : insurantModelList) {
            if (insurantModel.getInsSeq() == prodInsSeq) {
                //开始计算附加交通意外安驾宝保额
                // 查询历史单附加交通意外安驾宝保额
                double historyTrafficAmnt = insuredCoreRepository.getHistoryTrafficAccidentInsuAmnt(prtNo, insurantModel.getInsuredNo());
                //当前单附加交通意外安驾宝保额
                BigDecimal currentTrafficAmnt = this.getCurrentTrafficAmnt(prodInsSeq, requestModel.getProductInfo().getProducts().getProduct());
                double totalTrafficAmnt = currentTrafficAmnt.add(BigDecimal.valueOf(historyTrafficAmnt)).doubleValue();
                Date cvalidate = requestModel.getPolicyInfo().getCvalidate();
                int years = DateUtil.getYears(insurantModel.getBirthday(), cvalidate);
                //判断年龄和累积保额
                if (totalTrafficAmnt > 2000000 && years >= 18 && years <= 60) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NUGG0043_AAT070_MESSAGE);
                }
                if (years > 60 && totalTrafficAmnt > 1000000) {
                    return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NUGG0044_AAT070_MESSAGE);
                }
                //结束计算附加交通意外安驾宝保额
            }
        }
        // 校验安驾宝交通长期意外伤害保险的缴费期需与主险的缴费期是否一致
        // AAT007的这两个字段与主险进行判断
        Integer payYear = productModel.getPayYear();
        String payYearFlag = productModel.getPayYearFlag();
        Integer mainPayYear = 0;
        String mainPayYearFlag = "";
        ProductsModel productsModel = acceptRequestDataModel.getRequest().getProductInfo().getProducts();
        for (ProductModel product : productsModel.getProduct()) {
            if (redisService.isMainRisk(product.getProdCode())) {
                mainPayYear = product.getPayYear();
                mainPayYearFlag = product.getPayYearFlag();
                break;
            }
        }

        // 缴费期不一致
        if ((payYear.intValue() != mainPayYear.intValue()) || StringUtils.equals(payYearFlag, mainPayYearFlag)) {
            return AcceptUtil.getInsuredResponse(UnderWriteConstant.UNDER_WRITE_FIAL_CODE, UnderWriteConstant.UNDER_WRITE_NPGG0019_MESSAGE);
        }


        return AcceptUtil.getInsuredResponse(AcceptConstant.RESULT_SUCCESS_CODE, AcceptConstant.RESULT_SUCCESS_MESSAGE);
    }

    private BigDecimal getCurrentTrafficAmnt(int insSeq, List<ProductModel> productModelList) {
        BigDecimal total = BigDecimal.valueOf(0);
        for (ProductModel productModel : productModelList) {
            if (productModel.getProdInsSeq() == insSeq) {
                if (StringUtils.equals("AAT041", productModel.getProdCode()) ||
                        StringUtils.equals("AAT057", productModel.getProdCode()) ||
                        StringUtils.equals("AAT070", productModel.getProdCode())) {
                    total = total.add(productModel.getAmnt());
                }
            }
        }
        return total;
    }
}
